﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class PhatODAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from PhatO");
        }

        public PhatO GetById(String mapo)
        {
            PhatO phato = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from PhatO where MaPhatO = '" + mapo + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    phato = new PhatO();
                    phato.MaKenh = dataReader.GetString(0);
                    phato.MaTanSo = dataReader.GetString(1);
                    phato.ThoiGianPS = dataReader.GetDateTime(2);
                    return phato;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return phato;
        }

        public bool Insert(PhatO phato)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[PhatO]([MaKenh],[MaTanSo][ThoiGianPS])"
               + "VALUES('" + phato.MaKenh + "','" + phato.MaTanSo + "','" + phato.ThoiGianPS + "')";
            return Update(sql);
        }

        public bool DeleteById(String mapo)
        {
            return Update("delete from PhatO where MaPhatO = '" + mapo + "'");
        }

        public bool UpdateById(PhatO phato)
        {
            String sql = "update PhatO "
                 + " set ThoiGianPS = '" + phato.ThoiGianPS + "'"
                 + " where MaKenh = '" + phato.MaKenh + "' and MaTanSo = '" + phato.MaTanSo + "'";
            return Update(sql);
        }
    }
}
